/**
 * 
 */
package org.gaming.prefab.thing.longn;

import java.util.HashMap;
import java.util.Map;

import org.gaming.prefab.thing.IThingType;
import org.gaming.prefab.thing.ThingStorer;
import org.gaming.prefab.thing.cost.CostReceipt;
import org.gaming.prefab.thing.cost.ICostThing;
import org.gaming.prefab.thing.reward.IRewardThing;
import org.gaming.prefab.thing.reward.RewardReceipt;

/**
 * @author YY
 *
 */
public abstract class LongnThingStorer<RReceipt extends RewardReceipt<Long>, CReceipt extends CostReceipt<Long>>
		extends ThingStorer<Long, IRewardThing<Long>, ICostThing<Long>, RReceipt, CReceipt> {

	private static final Map<IThingType, LongnThingStorer<?, ?>> STORERS = new HashMap<>();
	
	public LongnThingStorer() {
		STORERS.put(thingType(), this);
	}
	
	public static LongnThingStorer<?, ?> getStorer(IThingType thingType) {
		return STORERS.get(thingType);
	}
	
	@Override
	protected boolean isEnough(long entityId, ICostThing<Long> thing) {
		requireCostPositive(thing);
		long remainCount = getCount(entityId, thing.getThingId()) - thing.getNumber();
		return remainCount >= 0;
	}

	@Override
	protected void requireCostPositive(ICostThing<Long> cost) {
		if(cost.getNumber() <= 0) {
    		throw new RuntimeException("物品数量必须大于0，实际：" + cost.getNumber());
    	}
	}

	@Override
	protected void requireRewardPositive(IRewardThing<Long> reward) {
		if(reward.getNumber() <= 0) {
    		throw new RuntimeException("物品数量必须大于0，实际：" + reward.getNumber());
    	}
	}
}
